/*
 * Copyright (C) 2014 VenomVendor <info@VenomVendor.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.bocai.dahuatech.ui.adapter;

import android.content.Intent;
import android.support.v4.app.FragmentActivity;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bocai.dahuatech.Bean.NewsListBean;
import com.bocai.dahuatech.R;
import com.bocai.dahuatech.ui.activity.NewsDetailActivity;
import com.bumptech.glide.Glide;

import java.util.List;

public class NewsAdapterHolder extends RecyclerView.Adapter<NewsAdapterHolder.ViewHolder> {

    OnItemClickListener mItemClickListener;
    private FragmentActivity mActivity;
    List<NewsListBean.ContentEntity> datas;


    public NewsAdapterHolder(FragmentActivity mActivity, List<NewsListBean.ContentEntity> datas) {
        this.mActivity = mActivity;
        this.datas=datas;
    }
    public void addDatas(List<NewsListBean.ContentEntity> beans){
        datas.addAll(beans);
        this.notifyDataSetChanged();

    }
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        final LayoutInflater mInflater = LayoutInflater.from(parent.getContext());
        final View sView = mInflater.inflate(R.layout.news_list_item, parent, false);
        return new ViewHolder(sView);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        holder.tvTitle.setText(datas.get(position).getTitle());
        holder.tvWriter.setText(mActivity.getResources().getString(R.string.publisher)+datas.get(position).getAuthor());
        holder.tvTime.setText(mActivity.getResources().getString(R.string.releasedate)+datas.get(position).getCreateTime());
        Glide.with(mActivity).load(datas.get(position).getImageUrl())
//                .error(R.mipmap.product_photo_defalt)
//                .placeholder(R.mipmap.product_photo_defalt)
                .into(holder.ivImage);
        holder.ll_itemview.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(mActivity, NewsDetailActivity.class);
                NewsListBean.ContentEntity data=datas.get(position);
                intent.putExtra("bean",data);
                intent.putExtra("what",1);
                mActivity.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    public void SetOnItemClickListener(OnItemClickListener mItemClickListener) {
        this.mItemClickListener = mItemClickListener;
    }


    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        ImageView ivImage;
        TextView tvTitle;
        TextView tvWriter;
        TextView tvTime;
        LinearLayout ll_itemview;
        public ViewHolder(View view) {
            super(view);
            ivImage= (ImageView) view.findViewById(R.id.iv_news_pic);
            tvTitle= (TextView) view.findViewById(R.id.tv_news_title);
            tvWriter= (TextView) view.findViewById(R.id.tv_news_writer);
            tvTime= (TextView) view.findViewById(R.id.tv_news_time);
            ll_itemview= (LinearLayout) view.findViewById(R.id.ll_itemview);
            view.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (mItemClickListener != null) {
                mItemClickListener.onItemClick(v, getLayoutPosition());
//                mItemClickListener.onItemClick(v, getLayoutPosition());
            }
        }

    }

}
